package cn.ztydata.teachingsystem.heaven.service;

import cn.ztydata.teachingsystem.common.Page;
import cn.ztydata.teachingsystem.exception.ServiceException;
import cn.ztydata.teachingsystem.heaven.common.WebContext;
import cn.ztydata.teachingsystem.heaven.cons.GroupType;
import cn.ztydata.teachingsystem.heaven.cons.Roles;
import cn.ztydata.teachingsystem.heaven.dao.ClassGroupDao;
import cn.ztydata.teachingsystem.heaven.dao.CourseGroupDao;
import cn.ztydata.teachingsystem.heaven.dao.NoticeDao;
import cn.ztydata.teachingsystem.heaven.dao.SpecialtyGroupDao;
import cn.ztydata.teachingsystem.heaven.entity.Notice;
import cn.ztydata.teachingsystem.heaven.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 通知服务类
 *
 * @author lyj
 * @since 2015-3-18
 */
@Service
public class NoticeService extends BaseService {
    @Autowired
    private NoticeDao noticedao;
    @Autowired
    private WebContext webContext;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private ClassGroupDao classGroupDao;
    @Autowired
    private SpecialtyGroupDao specialtyGroupDao;
    @Autowired
    private CourseGroupDao courseGroupDao;
    @Autowired
    private SpecialtyGroupService specialtyGroupService;


    //<editor-fold desc="验证">
    /**
     * "增删改"操作的角色验证
     *
     * @return void
     * @author lyj
     * @since 2015-3-18
     */
    public void check(final int groupType){
        Boolean checkRole = true;
        //权限验证
        if (groupType == GroupType.CLASS_GROUP || groupType == GroupType.COURSE_GROUP) {
            if (webContext.getRole() != Roles.TEACHER) {
                checkRole = false;
            }
        } else if (groupType == GroupType.SPEC_GROUP) {
            if (webContext.getRole() != Roles.SPEC_LEADER) {
                checkRole = false;
            }
        }

        if (!checkRole){
            log.warn("当前登录用户没有权限使用该功能！");
            throw new ServiceException("权限错误！");
        }
    }


    /**
     * "增删改"操作的创建人等。。。验证
     * @param id 通知ID
     * @param groupType 群组类型
     * @param groupId 群组ID
     * @param userNumber 登录用户工号（学号）
     * @return void
     * @author lyj
     * @since 2015-5-7
     */
    public void checkCreator(final long id, final int groupType, final long groupId, final String userNumber) {

        //获取通知明细
        Notice notice = noticedao.getNoticeDetail(id);
        if (notice == null) {
            log.debug("如果notice == null");
            throw new ServiceException("该通知已经被删除！");
        }
        Boolean checkRole = true;


        //如果是课程群组
        if (groupType == GroupType.CLASS_GROUP) {
            //验证是否为通知创建人
            if (webContext.getUserId().intValue() != notice.getCreatorId().intValue()) {
                checkRole = false;
            }
        } else if (groupType == GroupType.CLASS_GROUP) { //如果是班级群组
            //验证是否为该班的班主任
            String teacherNum = classGroupDao.getTeacherCodeById(groupId);
            if (teacherNum == null) {
                checkRole = false;
            } else {
                if (groupType != notice.getGroupType() || groupId != notice.getGroupId() || !teacherNum.equals(userNumber)) {
                    checkRole = false;
                }
            }
        } else if (groupType == GroupType.SPEC_GROUP) { //如果是专业群组
            //验证是否为专业负责人
            if (webContext.getUserId().intValue() != specialtyGroupDao.getLeaderById(groupId).intValue()) {
                checkRole = false;
            }
        }

        if (!checkRole) {
            log.warn("当前登录用户没有权限使用该功能！");
            throw new ServiceException("权限错误！");
        }
    }


    /**
     * "查询"操作的角色验证
     *
     * @return void
     * @author lyj
     * @since 2015-3-18
     */
    public void getCheckRole(final int groupType, final long groupId, final User userInfo) {
        Boolean checkRole = true;
        //验证课程群组权限
        if (groupType == GroupType.COURSE_GROUP ) {
            if (webContext.getRole() != Roles.TEACHER && webContext.getRole() != Roles.STUDENT) {
                checkRole = false;
            }

            //判断登录用户是否可以获取该课程群组信息
            if (courseGroupDao.checkGroupIdIsNum(groupId, userInfo.getNumber()) == 0) {
                checkRole = false;
            }

        } else if (groupType == GroupType.CLASS_GROUP) { //验证班级群组权限
            if (webContext.getRole() != Roles.STUDENT && webContext.getRole() != Roles.TEACHER) {
                checkRole = false;
            }

            //判断登录用户是否可以获取该班级群组信息
            if (classGroupDao.checkGroupIdIsNum(groupId, userInfo.getNumber()) == 0) {
                checkRole = false;
            }

        } else if (groupType == GroupType.SPEC_GROUP) { //验证专业群组权限
            if (webContext.getRole() != Roles.SPEC_LEADER && webContext.getRole() != Roles.TEACHER) {
                checkRole = false;
            }

            //判断登录用户是否可以获取该专业群组信息
            if (!specialtyGroupService.getSpecialtyIds(userInfo).contains(groupId)) {
                checkRole = false;
            }
        }

        if (!checkRole){
            log.warn("当前登录用户没有权限使用该功能！");
            throw new ServiceException("权限错误！");
        }
    }
    //</editor-fold>

    //<editor-fold desc="新增">
    /**
     * 新增通知
     *
     * @param notice 通知实体
     * @return void
     * @author lyj
     * @since 2015-3-18
     */
    public void addNotice(Notice notice) {

        //验证权限
        check(notice.getGroupType());

        if (noticedao.addNotice(notice) != 1) {
            log.debug("新增通知失败noticedao.addNotice(notice) != 1");
            log.warn("新增通知失败!");

            throw new ServiceException("新增失败！");
        }
    }
    //</editor-fold>

    //<editor-fold desc="删除">

    /**
     * 删除通知
     *
     * @param id 通知ID
     * @return void
     * @author lyj
     * @since 2015-3-18
     */
    public void deleteNotice(final long id, final int groupType, final long groupId, final User userInfo) {
        //验证角色
        check(groupType);

        //判断创建人等权限
        checkCreator(id, groupType, groupId, userInfo.getNumber());

        if (noticedao.deleteNotice(id) != 1) {
            log.debug("删除通知失败noticedao.deleteNotice(id) != 1");
            log.warn("删除通知失败!");

            throw new ServiceException("删除失败！");
        }
    }
    //</editor-fold>

    //<editor-fold desc="编辑">
    /**
     * 编辑通知
     *
     * @param notice 通知实体
     * @param userInfo 登录用户信息
     * @return void
     * @author lyj
     * @since 2015-3-18
     */
    public void editNotice(final Notice notice, final User userInfo){
        //验证角色
        check(notice.getGroupType());

        //判断登录用户是否为创建人
        checkCreator(notice.getId(), notice.getGroupType(), notice.getGroupId(), userInfo.getNumber());

        if (noticedao.editNotice(notice) != 1){
            log.warn("编辑通知失败!");

            throw new ServiceException("编辑失败！");
        }
    }
    //</editor-fold>

    //<editor-fold desc="查询">
    /**
     * 获取通知明细
     *
     * @param id 通知ID
     * @return
     * @author lyj
     * @since 2015-3-18
     */
    public Notice getNoticeDetail(final long id, final int groupType, final long groupId, final User userInfo) {

        //验证角色
        getCheckRole(groupType, groupId, userInfo);

        //判断通知是否已经被删除
        Notice notice = noticedao.getNoticeDetail(id);
        if (notice == null) {
            log.warn("获取通知明细失败!");
            throw new ServiceException("该通知已经被删除!");
        }

        return notice;
    }



    /**
     * 获取通知列表
     *
     * @param groupType 群组类型
     * @param groupId 群组ID
     * @param pageNum 当前页码
     * @param pageSize 每页条数
     * @return
     * @author lyj
     * @since 2015-3-18
     */
    public Page<Notice> getNotices(final int groupType, final long groupId, final int pageNum, final int pageSize, final User userInfo) {
        //验证角色
        getCheckRole(groupType, groupId, userInfo);

        log.debug("2.获取通知列表");
        return noticedao.getNotices(groupType, groupId, pageNum, pageSize);

    }
    //</editor-fold>


}
